package com.linq.cool.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linq.cool.common.constants.UserConstants;
import com.linq.cool.common.core.domain.BaseController;
import com.linq.cool.common.core.domain.PageVO;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.core.utils.SecurityUtils;
import com.linq.cool.common.log.annotation.Log;
import com.linq.cool.common.log.enums.BusinessType;
import com.linq.cool.common.security.annotation.PreAuthorize;
import com.linq.cool.system.domain.SysNotice;
import com.linq.cool.system.dto.SysNoticeQueryDTO;
import com.linq.cool.system.service.SysNoticeService;
import com.linq.cool.system.vo.SysNoticeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2021/1/28 09:54
 * @Description:
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("notice")
public class SysNoticeController extends BaseController {
    @Autowired
    private SysNoticeService sysNoticeService;

    /**
     * 分页条件查询公告信息
     *
     * @param dto
     *
     * @return
     */
    @PreAuthorize(hasPerm = "sys:notice:list")
    @GetMapping("list")
    public Result<?> list(SysNoticeQueryDTO dto) {
        IPage<SysNoticeVO> page = sysNoticeService.listByPage(dto.getPage(), dto);
        return Result.ok(new PageVO().setTotal(page.getTotal()).setRecords(page.getRecords()));
    }

    /**
     * 根据通知公告编号获取详细信息
     *
     * @param noticeId 编号
     *
     * @return R
     */
    @GetMapping("/{noticeId}")
    public Result<?> getInfo(@PathVariable("noticeId") Long noticeId) {
        return Result.ok(sysNoticeService.getById(noticeId));
    }


    /**
     * 新增通知公告
     *
     * @param notice SysNotice
     *
     * @return R
     */
    @PreAuthorize(hasPerm = "sys:notice:add")
    @Log(moduleTitle = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<?> add(@Validated @RequestBody SysNotice notice) {
        notice.setCreator(SecurityUtils.getUserId());
        return this.toResult(sysNoticeService.save(notice));
    }


    /**
     * 修改通知公告
     *
     * @param notice SysNotice
     *
     * @return R
     */
    @PreAuthorize(hasPerm = "sys:notice:edit")
    @Log(moduleTitle = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<?> edit(@Validated @RequestBody SysNotice notice) {
        notice.setGmtModified(new Date()).setModifier(SecurityUtils.getUserId());
        return this.toResult(sysNoticeService.updateById(notice));
    }


    /**
     * 删除通知公告
     *
     * @param noticeIds List<Integer> 编号集合
     *
     * @return AjaxResult
     */
    @PreAuthorize(hasPerm = "sys:notice:remove")
    @Log(moduleTitle = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public Result<?> remove(@PathVariable("noticeIds") List<Long> noticeIds) {
        return this.toResult(sysNoticeService.removeByIds(noticeIds));
    }

    /**
     * 获取通知公告列表
     *
     * @return
     */
    @GetMapping("/home/list")
    public Result<?> list() {
        return Result.ok(sysNoticeService.list(new LambdaQueryWrapper<SysNotice>()
                                                       .select(SysNotice::getId, SysNotice::getTitle)
                                                       .eq(SysNotice::getStatus, UserConstants.StateEnum.STATUS_NORMAL.getCode())
                                                       .orderByAsc(SysNotice::getId)
        ));
    }

}
